package com.glory.good_dark.activity;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.good_dark.R;
import com.glory.good_dark.adapter.CapacityMangerListAdapter;
import com.glory.good_dark.application.App;
import com.glory.good_dark.entity.response.BaseEntity;
import com.glory.good_dark.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class CapacityMangerActivity extends BaseProjectActivity {

    @BindView(R.id.tvUser)
    TextView tvUser;
    @BindView(R.id.EQPID_Txt)
    TextView EQPIDTxt;
    @BindView(R.id.tvAddEqp)
    TextView tvAddEqp;
    @BindView(R.id.rvBindEqpList)
    RecyclerView rvBindEqpList;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    private CapacityMangerListAdapter mAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_capacity_manger;
    }

    @Override
    protected void initView() {
        tvUser.setText(App.userEntity.getWORKNO());
        initRecyclerView(rvBindEqpList, this);
        mAdapter = new CapacityMangerListAdapter(null);
        mAdapter.isFirstOnly(true);
        mAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        rvBindEqpList.setAdapter(mAdapter);
        mAdapter.setOnDeleteListener((item, i) -> {
            JsonObject map = new JsonObject();
            map.addProperty("OPERATOR",App.userEntity.getWORKNO());
            map.addProperty("EQPID",item.getEQP_ID());
            requestUnBind(map,i,"PDAPersonnelUnBindingEQP");
        });

    }

    @Override
    protected void initData() {
        FreeUI_EntityCallBack<BaseEntity<PersonBindInfoEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseEntity<PersonBindInfoEntity>>
                (new TypeToken<BaseEntity<PersonBindInfoEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess
                    (final Response<BaseEntity<PersonBindInfoEntity>> response) {
                if (response.body().isSuccess(getApplicationContext(),getSupportFragmentManager())) {
                    List<PersonBindInfoEntity.PersonEqp> eqpidlist = response.body().getData().getBody().getEQPIDLIST();
                    if (eqpidlist.size() > 0){
                        mAdapter.setNewData(eqpidlist);
                    }
                }

            }

            @Override
            public void onError
                    (Response<BaseEntity<PersonBindInfoEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "PDAPersonnelBindingInfo");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("OPERATOR",App.userEntity.getWORKNO());
        NetUtils.requestPostNet(App.WEB_URL + "/api/PDA",this,callBack,"PDAPersonnelBindingInfo",map);
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("产能管理");
        freeUI_titleView.setRightListener("提交", v -> {
            if (mAdapter.getData().size() <= 0){
                showToast("请先绑定设备",false);
                return;
            }
            JsonObject map = new JsonObject();
            map.addProperty("OPERATOR",App.userEntity.getWORKNO());
            map.add("EqpBindList",new JsonParser().parse(new Gson().toJson(mAdapter.getData())).getAsJsonArray());
            requestUnBind(map,-1,"PDAPersonnelBindingSubmit");
        });
        return new FreeUI_AddViewEntity(getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY), llMain);
    }

    private void requestUnBind(JsonObject map,int position,String messageName) {
        FreeUI_EntityCallBack<BaseEntity<PersonBindInfoEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseEntity<PersonBindInfoEntity>>
                (new TypeToken<BaseEntity<PersonBindInfoEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess
                    (final Response<BaseEntity<PersonBindInfoEntity>> response) {
                if (response.body().isSuccess(getApplicationContext(),getSupportFragmentManager())) {
                    if (position == -1){
                        showConfirmDialog(CapacityMangerActivity.this,"提交成功");
                    }else {
                        mAdapter.remove(position);
                        mAdapter.notifyDataSetChanged();
                    }

                }

            }

            @Override
            public void onError
                    (Response<BaseEntity<PersonBindInfoEntity>> response) {
                super.onError(response);
                loadError(response.getException(), messageName);
            }
        };

        NetUtils.requestPostNet(App.WEB_URL + "/api/PDA",this,callBack,messageName,map);
    }

    @Override
    protected void EQPReplaceSuccess(int tag, String content) {
        if (tag == 10010){
            EQPIDTxt.setText(content);
        }


    }

    @OnClick({R.id.tvAddEqp,R.id.EQPID_Txt})
    public void onViewClicked(View view) {
        if (view.getId() == R.id.EQPID_Txt){
            showSpanDialog(10010,"请扫描设备编号");
        }else if (view.getId() == R.id.tvAddEqp){
            if (EQPIDTxt.getText().toString().length() <= 0){
                showToast("请先扫描设备编号",false);
                return;
            }
            for (PersonBindInfoEntity.PersonEqp entity : mAdapter.getData()){
                if (entity.getEQP_ID().equals(EQPIDTxt.getText().toString())){
                    showToast("当前设备已在列表中",false);
                    return;
                }
            }
            PersonBindInfoEntity.PersonEqp personEqp = new PersonBindInfoEntity.PersonEqp();
            personEqp.setEQP_ID(EQPIDTxt.getText().toString());
            mAdapter.addData(personEqp);
            EQPIDTxt.setText("");
        }

    }

    public static class PersonBindInfoEntity implements Serializable{
        private String OPERATOR;
        private List<PersonEqp> EQPIDLIST;

        public String getOPERATOR() {
            return OPERATOR == null ? "" : OPERATOR;
        }

        public void setOPERATOR(String OPERATOR) {
            this.OPERATOR = OPERATOR;
        }

        public List<PersonEqp> getEQPIDLIST() {
            if (EQPIDLIST == null) {
                return new ArrayList<>();
            }
            return EQPIDLIST;
        }

        public void setEQPIDLIST(List<PersonEqp> EQPIDLIST) {
            this.EQPIDLIST = EQPIDLIST;
        }

        public static class PersonEqp implements Serializable{
            private String EQP_ID;

            public String getEQP_ID() {
                return EQP_ID == null ? "" : EQP_ID;
            }

            public void setEQP_ID(String EQP_ID) {
                this.EQP_ID = EQP_ID;
            }
        }
    }
}